package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;

import dao.MenuDao;
import dao.OrdersDao;
import modle.Country;
import modle.Menu;
import util.DbUtil;
import view.Customer_Menu.MyTableModel;

import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.Toolkit;
import javax.swing.JComboBox;

public class View_Items extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JLabel lblViewItems;
	private JButton btnBack;
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	ResourceBundle rd;
	private Object[][] rowData = new Object[1000][3];
	/**
     * 表头（列名）
     */
    private Object[] columnNames = {"菜品的名称", "菜品的编号", "菜品的价格"};

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					View_Items frame = new View_Items();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public View_Items() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(View_Items.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 585, 601);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblViewItems = new JLabel("View Items", JLabel.CENTER);
		lblViewItems.setForeground(new Color(205, 133, 63));
		lblViewItems.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 23));
		lblViewItems.setBounds(161, 13, 236, 39);
		contentPane.add(lblViewItems);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 164, 539, 309);
		contentPane.add(scrollPane);
		
		table = new JTable(new MyTableModel());
		table.setRowHeight(25);										//设置table行高
		table.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		scrollPane.setViewportView(table);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(View_Items.class.getResource("/img/menus.png")));
		lblNewLabel.setBounds(161, 47, 246, 100);
		contentPane.add(lblNewLabel);
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 17));
		btnBack.setBounds(14, 497, 82, 33);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Admin_Menu().setVisible(true);
				dispose();
			}
		});
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(438, 20, 98, 32);
		contentPane.add(comboBox);
		
		fillCountries();
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		lblViewItems.setText(rd.getString("lblViewItems"));
		btnBack.setText(rd.getString("btnBack"));
	}
	
	public class MyTableModel extends AbstractTableModel {
        /**
         * 表格所有行数据
         */
			
		private int i, num = 0;
        
        public MyTableModel() {
//        	Orders orders = null;
        	ResultSet rs;
        	
    		for(i=1;  ; ++i)
    		{
    			try {
    				Menu menu = MenuDao.MenuSearch2(DbUtil.getCon(), i);
    				if(menu!=null) {
    					rowData[i-1][0] = menu.getName();
    					rowData[i-1][1] = menu.getId();
    					rowData[i-1][2] = menu.getPrice() + "￥";
    				}
    				else   break;
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
//        	return rowData;
        }

		/**
         * 返回总行数
         */
        @Override
        public int getRowCount() {
            return i-1;
        }

        /**
         * 返回总列数
         */
        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        /**
         * 返回列名称（表头名称），AbstractTableModel 中对该方法的实现默认是以
         * 大写字母 A 开始作为列名显示，所以这里需要重写该方法返回我们需要的列名。
         */
        @Override
        public String getColumnName(int column) {
            return columnNames[column].toString();
        }

        /**
         * 返回指定单元格的显示的值
         */
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return rowData[rowIndex][columnIndex];
        }
    }
}
